package com.framework.service.app.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.app.AppDiscoverArticleDao;
import com.framework.entity.or.AppDiscoverArticle;
import com.framework.entity.or.AppDiscoverCover;
import com.framework.entity.or.OrBanner;
import com.framework.entity.sys.SysAgent;
import com.framework.exception.RRException;
import com.framework.page.PageUtils;
import com.framework.page.Query;
import com.framework.service.AgentService;
import com.framework.service.BannerService;
import com.framework.service.app.AppDiscoverArticleService;
import com.framework.service.app.AppDiscoverCoverService;
import com.framework.utils.MapObjUtils;
import com.framework.validator.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wq
 */
@Service("appDiscoverArticleService")
public class AppDiscoverArticleServiceImpl extends ServiceImpl<AppDiscoverArticleDao, AppDiscoverArticle> implements AppDiscoverArticleService {


    @Autowired
    private AgentService agentService;
    @Autowired
    private BannerService bannerService;
    @Autowired
    private AppDiscoverCoverService appDiscoverCoverService;

    @Override
    public List<Map<String, Object>> getArticleListByDeptId(Long deptId, Integer pageNo) throws IllegalAccessException {
        if (pageNo == null || pageNo.intValue() == 0) {
            throw new RRException("9000", "页数不能为空或者位0");
        }
        List<AppDiscoverArticle> list = baseMapper.queryListByLimit(deptId, (pageNo - 1) * 10);

        List<AppDiscoverCover> coverList;

        List<Map<String, Object>> result = new ArrayList<>();
        for (AppDiscoverArticle article : list) {
            coverList = appDiscoverCoverService.selectList(new EntityWrapper<AppDiscoverCover>()
                    .eq("discover_id", article.getArticleId())
                    .orderBy("sort", true)
            );
            article.setJumpUrl(article.getJumpUrl().replace("{id}", article.getArticleId().toString()));
            article.setContent("");
            if (coverList.size() > 0) {
                Map<String, Object> temp = MapObjUtils.objToMapNoTranTime(article);
                temp.put("coverList", coverList);
                result.add(temp);
            }

        }
        return result;
    }

    @Override
    public Map<String, Object> getDiscoverInfo(String token, Integer pageNo) throws IllegalAccessException {
        if (pageNo == null || pageNo.intValue() == 0) {
            throw new RRException("9000", "页数不能为空或者为@0");
        }
        SysAgent agent = agentService.queryAgentByToken(token);
        Long deptId = agent.getDeptId();
        Map<String, Object> result = new HashMap<>(5);

        if (pageNo == 1) {
            Map<String, Object> map = getDiscoverHeadInfoByDeptId(deptId);
            result.putAll(map);
        }
        List<Map<String, Object>> list = getArticleListByDeptId(deptId, pageNo);
        result.put("list", list);
        return result;
    }

    @Override
    public PageUtils queryArticlePage(Map<String, Object> params) {
        Integer state = 0;

        if (params.get("state") != null) {
            state = Integer.valueOf(params.get("state").toString());
        }
        Integer type = 0;

        if (params.get("type") != null) {
            type = Integer.valueOf(params.get("type").toString());
        }

        Page<AppDiscoverArticle> page = selectPage(
                new Query<AppDiscoverArticle>(params).getPage(), new EntityWrapper<AppDiscoverArticle>()
                        .eq("dept_id", params.get("deptId"))
                        .eq(state != 0, "state", state)
                        .eq(type != 0, "type", type)
                        .orderBy("create_time", false)
        );
        for (AppDiscoverArticle record : page.getRecords()) {
            record.setContent(record.getContent().replace(", &quot;Microsoft YaHei&quot;", ""));
        }
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryImgListByArticleId(Map<String, Object> params) {
        Page<AppDiscoverCover> page = appDiscoverCoverService.selectPage(new Query<AppDiscoverCover>(params).getPage(),
                new EntityWrapper<AppDiscoverCover>()
                        .eq("discover_id", params.get("id"))
        );

        return new PageUtils(page);
    }

    @Override
    public Integer addArticle(AppDiscoverArticle article) {
        baseMapper.insert(article);
        AppDiscoverArticle temp = selectOne(new EntityWrapper<AppDiscoverArticle>()
                .eq("type", article.getType())
                .eq("dept_id", article.getDeptId()).eq("write_date", article.getWriteDate())
                .eq("title", article.getTitle())

        );
        return temp.getArticleId();
    }

    @Override
    public void deleteArticleById(Integer articleId) {
        Assert.isNull(articleId, "id不能为空");
        appDiscoverCoverService.delete(new EntityWrapper<AppDiscoverCover>().eq("discover_id", articleId));
        baseMapper.deleteById(articleId);
    }


    @Override
    public Map<String, Object> getDiscoverHeadInfoByDeptId(Long deptId) {

        List<OrBanner> orBannerList = bannerService.queryBannerList(2, deptId);

        List<OrBanner> wonders = bannerService.queryBannerListLimit(6, 3, deptId);

        Map<String, Object> returnMap = new HashMap<>(2);
        returnMap.put("bannerList", orBannerList);
        returnMap.put("wonderList", wonders);
        return returnMap;
    }

}
